//
//  CSNewChangePhoneViewController.m
//  ZSYH
//
//  Created by 主事丫环 on 2019/4/23.
//  Copyright © 2019 魏堰青. All rights reserved.
//

#import "CSNewChangePhoneViewController.h"
#import "DPSendCodeButton.h"

@interface CSNewChangePhoneViewController ()
@property (weak, nonatomic) IBOutlet UITextField *myyzmTF;
@property (weak, nonatomic) IBOutlet UITextField *myphoneTF;
@property (weak, nonatomic) IBOutlet YZMButton *yzmBut;

@end

@implementation CSNewChangePhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改绑定手机号";
    [self.leftbutton setTitle:@"取消" forState:(UIControlStateNormal)];
    [self.rightbutton setTitle:@"确定" forState:(UIControlStateNormal)];
    self.myphoneTF.keyboardType = UIKeyboardTypeNumberPad;
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    
    
    
    DPSendCodeButton *sendCodeButton = [[DPSendCodeButton alloc]initWithFrame:self.yzmBut.bounds identify:@"LoginViewButton"];
    //    sendCodeButton.backgroundColor = [UIColor redColor];
    [sendCodeButton setTitleColor:zhutiColor forState:(UIControlStateNormal)];
    sendCodeButton.titleLabel.font = FontSize(13);
    [sendCodeButton addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [self.yzmBut addSubview:sendCodeButton];
    self.yzmBut.userInteractionEnabled = NO;
    
    //实时获得输入内容
    [self.myphoneTF addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];
    
}
- (void)textFieldDidChange{
    NSString *phoneStr = [_myphoneTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if ([HFTools validateMobile:phoneStr]) {
        self.yzmBut.userInteractionEnabled = YES;
    }else{
        self.yzmBut.userInteractionEnabled = NO;
    }
}
-(void)click{
    NSString *phoneStr = [_myphoneTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (![self isMobileNumberOnly:phoneStr]) {
        ShowToastWithText(@"请输入正确的手机号");
        return;
    }
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"phone"] = phoneStr;
    
    
    [NetWorkTool POST:sendMsg param:param success:^(id dic) {
        KMyLog(@"fasonbgyanzhnegamn%@",dic);
        ShowToastWithText(@"验证码发送成功");
        [self.myphoneTF resignFirstResponder];
        [self.myyzmTF becomeFirstResponder];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:NO];
}
- (IBAction)sendMAgAction:(YZMButton *)sender {
    
    
    
    return;
//    if (![self isMobileNumberOnly:_myphoneTF.text]) {
//        ShowToastWithText(@"请输入正确的手机号");
//        return;
//    }
//    [sender timeFailBeginFrom:60];
//
//    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
//    param[@"phone"] =_myphoneTF.text;
//
//
//    [NetWorkTool POST:sendMsg param:param success:^(id dic) {
//        KMyLog(@"fasonbgyanzhnegamn%@",dic);
//        ShowToastWithText(@"验证码发送成功");
//        [self.myphoneTF resignFirstResponder];
//        [self.myyzmTF becomeFirstResponder];
//    } other:^(id dic) {
//
//    } fail:^(NSError *error) {
//
//    } needUser:NO];
}


- (void)rightClick{
    NSString *phoneStr = [_myphoneTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *codeStr = [_myyzmTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (![HFTools validateMobile:phoneStr]) {
        ShowToastWithText(@"请输入正确的手机号");
        return;
    }else if (codeStr.length < 1){
        ShowToastWithText(@"请输入验证码");
        return;
    }
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"phone"] = phoneStr;
    param[@"yzCode"] = codeStr;
    
    [NetWorkTool POST:changeInfo param:param success:^(id dic) {
        ShowToastWithText(@"修改成功");
        [self.navigationController popViewControllerAnimated:YES];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
    
}
///判断是否为电话号码
- (BOOL)isMobileNumberOnly:(NSString *)mobileNum
{
    
    NSString * MOBILE = @"^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    
    if ([regextestmobile evaluateWithObject:mobileNum] == YES)
    {
        return YES;
    }
    else
    {
        return NO;
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
